package io.github.lee.jel.global.http.interceptors

import android.content.Context
import android.os.Build
import io.github.lee.core.repository.di.http.HttpModule
import io.github.lee.jel.BuildConfig
import okhttp3.Interceptor
import okhttp3.Response
import okhttp3.logging.HttpLoggingInterceptor

fun resetHttp(
    host: String?, username: String?,token:String?=null
) {
    val list = mutableListOf<Interceptor>()
        .also {
            it.add(PublicHeaderInterceptor(username ?: ""))
            it.add(PublicParameterInterceptor(token))
            it.add(HttpLoggingInterceptor().also { interceptor ->
                interceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
            }
            )
        }
    HttpModule.init(host ?: "", list)
}

class PublicHeaderInterceptor(private val username: String) : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        val client = "Android"
        val device = Build.MANUFACTURER + " " + Build.MODEL
        val deviceId = "$username $device"
        val version = BuildConfig.VERSION_NAME

        val auth =
            "Emby UserId=\"$username\", Client=\"$client\", Device=\"$device\", DeviceId=\"$deviceId\", Version=\"$version\""

        return chain.proceed(
            chain.request()
                .newBuilder()
                .addHeader("Authorization", auth)
                .build()
        )
    }

}